#!/usr/bin/perl
# 
# Exercise 8.5
# 
# Write a program that searches by name for a gene in a hash. Get the genes from 
# your own work or try downloading a list of all genes for a given organism from 
# www.ncbi.nlm.nih.gov or one of the web sites given in Appendix A. Make a hash of 
# all the genes (key=name, value=gene ID or sequence). Hint: you may have to write 
# a short Perl program to reformat the list of genes you start with to make it easy to 
# populate the Perl hash.
#
# Answer to Exercise 8.5

# There are, obviously, many ways this exercise could be done.  Here's one way:

# I went to the NCBI website mentioned in the question, and on the LocusLink page
# I asked for all human links with keyword "cancer".  I chose "Save Loci" and got
# a report on my computer that needed to have the information extracted.  Here's
# the first record (commented out with # signs):

#LOCUSID: 9429
#LOCUS_CONFIRMED: yes
#LOCUS_TYPE: gene with protein product, function known or inferred
#ORGANISM: Homo sapiens
#STATUS: REVIEWED
#NM: NM_004827|4757849|na
#NP: NP_004818|4757850
#CDD: ABC transporter|pfam00005|184|na|7.608430e+01
#PRODUCT: ATP-binding cassette, sub-family G, member 2
#ASSEMBLY: AF103796
#CONTIG: NT_022959
#EVID: none
#XM: XM_003630|14724933|na
#XP: XP_003630|12730365|na
#CDD: ABC transporter|pfam00005|314|6.336e-30|125.561
#EVID: none
#XM: XM_032420|14724936|na
#XP: XP_032420|14724937|na
#EVID: none
#XM: XM_032422|14724929|na
#XP: XP_032422|14724930|na
#EVID: none
#XM: XM_032423|14724949|na
#XP: XP_032423|14724950|na
#EVID: none
#XM: XM_032424|14724924|na
#XP: XP_032424|14724925|na
#EVID: none
#XM: XM_032425|15295111|na
#XP: XP_032425|14724955|na
#EVID: none
#XM: XM_055552|16157903|na
#XP: XP_055552|16157904|na
#EVID: none
#XM: XM_055553|16157906|na
#XP: XP_055553|16157907|na
#ACCNUM: AF093772|3747074|na
#TYPE: m
#ACCNUM: AF098951|12414050|na
#TYPE: m
#PROT: AAC97367|4038352
#ACCNUM: AF103796|4185795|na
#TYPE: m
#PROT: AAD09188|4185796
#ACCNUM: AK002040|7023680|na
#TYPE: m
#PROT: BAA92050|7023681
#OFFICIAL_SYMBOL: ABCG2
#OFFICIAL_GENE_NAME: ATP-binding cassette, sub-family G (WHITE), member 2
#ALIAS_SYMBOL: MXR
#ALIAS_SYMBOL: ABC1
#ALIAS_SYMBOL: ABCP
#ALIAS_SYMBOL: BCRP
#ALIAS_SYMBOL: MXR1
#ALIAS_SYMBOL: ABC15
#ALIAS_SYMBOL: EST157481
#PREFERRED_PRODUCT: ATP-binding cassette, sub-family G, member 2
#SUMMARY: Summary: The membrane-associated protein encoded by this gene is included in the superfamily of ATP-binding cassette (ABC) transporters. ABC proteins transport various molecules across extra- and intra-cellular membranes. ABC genes are divided into seven distinct subfamilies (ABC1, MDR/TAP, MRP, ALD, OABP, GCN20, White). This protein is a member of the White subfamily. Alternatively referred to as a breast cancer resistance protein, this protein functions as a xenobiotic transporter which may play a major role in multi-drug resistance. It likely serves as a cellular defense mechanism in response to mitoxantrone and anthracycline exposure. Significant expression of this protein has been observed in the placenta, which may suggest a potential role for this molecule in placenta tissue.
#CHR: 4
#STS: stSG9923|4|62742|na|seq_map|epcr
#STS: WI-14176|4|64270|na|seq_map|epcr
#STS: stSG2801|4|64580|na|na|epcr
#STS: stSG44051|-|66120|na|na|epcr
#COMP: 10090|Abcg2|6|6 28.50 cM|26357|4|ABCG2|ncbi_mgd
#COMP: 10090|Abcg2|6|6 28.50 cM|26357|4|ABCG2|ucsc_mgd
#ALIAS_PROT: placenta specific MDR protein
#ALIAS_PROT: mitoxantrone resistance protein
#ALIAS_PROT: breast cancer resistance protein
#UNIGENE: Hs.194720
#BUTTON: unigene.gif
#LINK: http://www.ncbi.nlm.nih.gov/UniGene/clust.cgi?ORG=Hs&CID=194720
#OMIM: 603756
#MAP: 4q22|RefSeq|C|
#MAPLINK: default_human_gene|ABCG2
#BUTTON: snp.gif
#LINK: http://www.ncbi.nlm.nih.gov/SNP/snp_ref.cgi?locusId=9429
#BUTTON: homol.gif
#LINK: http://www.ncbi.nlm.nih.gov/HomoloGene/homolquery.cgi?TEXT=9429[loc]
#BUTTON: gdb.gif
#LINK: http://gdbwww.gdb.org/gdb-bin/genera/accno?GDB:9956672
#BUTTON: ensembl.gif
#LINK: http://www.ensembl.org/Homo_sapiens/contigview?geneid=NM_004827
#BUTTON: ucsc.gif
#LINK: http://genome.ucsc.edu/cgi-bin/hgTracks?position=NM_004827
#BUTTON: proteome.gif
#LINK: http://www.proteome.com/databases/HumanPD/reports/9429.html
#DB_DESCR: GeneCards
#DB_LINK: http://bioinformatics.weizmann.ac.il/cards-bin/carddisp?ABCG2
#PMID: 10070941,9892175,9861027,9850061,8894702
#SUMFUNC: ATP binding cassette transporter G2|Proteome
#GO: molecular function|ATP binding|P|GO:0005524|Proteome|9850061
#GO: molecular function|transporter|E|GO:0005215|Proteome|9861027
#GO: biological process|drug resistance|E|GO:0009315|Proteome|9861027
#GO: biological process|small molecule transport|P|GO:0006832|Proteome|9850061
#GO: cellular component|integral membrane protein|P|GO:0016021|Proteome|9850061
#GO: molecular function|xenobiotic-transporting ATPase|E|GO:0008559|Proteome|9861027
#GO: molecular function|ATP-binding cassette (ABC) transporter|P|GO:0004009|Proteome|9850061
#EXTANNOT: biochemical function|Hydrolase|P|Proteome|9850061
#EXTANNOT: molecular localization|Integral membrane|P|Proteome|9850061
#EXTANNOT: biochemical function|ATP-binding cassette|P|Proteome|9850061
#EXTANNOT: subcellular localization|Unspecified membrane|P|Proteome|9850061
#EXTANNOT: biochemical function|Active transporter, primary|P|Proteome|9850061
#
# The whole search found 181 loci, and is almost .5Mb, over 10000 lines.  So here's
# a simple Perl program to extract the LocusID and the official gene name, and to
# print out a hash definition.  Note the <>, which means that the program assumes you
# will give the filename of the LocusLink output file on the command line.

##################################################
#
## There are plenty of ways to print make the hash, but this is probably the easiest.
## The Data::Dumper module is a very handy way to print out the contents of a data
## structure.  Again, the code is commented out.
#
#use Data::Dumper;
#
# use strict;
# use warnings;
#
#my %hash = ();
#my $gene_name = '';
#my $locus_id = '';
#
## Get first line to start things off
#$locus_id = <>;
#$locus_id = s/LOCUSID://;
#chomp($locus_id);
#
#while(<>) {
#	if(/LOCUSID:\s*(.*)\n/) {
#		# Store previous entry's information
#		$hash{$gene_name} = $locus_id;
#
#		# Get new locus id
#		$locus_id = $1;
#
#	}elsif(/OFFICIAL_SYMBOL:\s*(.*)\n/) {
#		$gene_name = $1;
#	}
#}
#
#print Dumper(\%hash);
#
#exit;
#
##################################################

use strict;
use warnings;

# Here's the output of that short Perl program - I just changed the 
# data structure from the form given, $VAR1 = { ... } (an anonymous
# hash reference), to  the form  my %hash = ( ... )

my %hash = (
          'RAD54L' => '8438',
          'MAGED2' => '10916',
          'PCA3' => '50652',
          'REV1L' => '51455',
          'ESR1' => '2099',
          'MAD1L1' => '8379',
          'APC' => '324',
          'CDKN2A' => '1029',
          'ELAC2' => '60528',
          'LIG3' => '3980',
          'LIG4' => '83748',
          'SDCCAG43' => '10812',
          'IRF1' => '3659',
          'MLH1' => '4292',
          'TNFSF15' => '9966',
          'LZTS1' => '29944',
          'MLH3' => '27030',
          'PCAP' => '50980',
          'MAGEE1' => '51438',
          'DRCC1' => '84417',
          'PPARG' => '5468',
          'EP300' => '2033',
          'CTAG1' => '1485',
          'SPANXA1' => '30014',
          'CTAG2' => '30848',
          'PES1' => '23481',
          'HPC1' => '5513',
          'DAB2' => '1601',
          'BCL10' => '8142',
          'DBCCR1' => '1620',
          'LDOC1' => '23641',
          'POLM' => '27434',
          'PTEN' => '5728',
          'TOC' => '11073',
          'SRC' => '6714',
          'PMS1' => '5378',
          'PMS2' => '5395',
          'CDK2AP1' => '8099',
          'DDX26' => '50514',
          'DLEC1' => '9940',
          'AGR2' => '10551',
          'HPCX' => '9566',
          'BCAR1' => '9564',
          'BIN1' => '274',
          'BRCA1' => '672',
          'BCAR2' => '9565',
          'BIN2' => '51411',
          'BRCA2' => '25855',
          'BCAR3' => '8412',
          'ARHI' => '9077',
          'CDH1' => '999',
          'BUB1' => '699',
          'KRAS2' => '8169',
          'TP53BP1' => '11236',
          'SDCCAG1' => '9147',
          'SDCCAG3' => '10807',
          'NRAS' => '4893',
          'RAD51' => '5888',
          'SDCCAG8' => '10806',
          'C20orf1' => '22974',
          'PPP2R1B' => '5519',
          'POV1' => '23519',
          'BCAS1' => '8537',
          'HIC1' => '54118',
          'KAI1' => '3732',
          'BCAS2' => '10286',
          'DCC' => '1630',
          'AMPH' => '273',
          'NBS1' => '4683',
          'ST8' => '6765',
          'MUC16' => '94025',
          'BAP1' => '8314',
          'TP53' => '7157',
          'BAX' => '51742',
          'CTDP1' => '9150',
          'BARD1' => '580',
          'GPR68' => '10403',
          'MRPS26' => '64949',
          'SSTR2' => '6752',
          'C17orf28' => '80791',
          'STUB1' => '10273',
          'SSX4' => '6759',
          'DLC1' => '10395',
          'FGF4' => '11334',
          'ARMET' => '29108',
          'ATM' => '472',
          'ABCG2' => '',
          'LCO' => '3935',
          'MADH4' => '4089',
          'SLC22A1L' => '5002',
          'PCANAP1' => '50630',
          'PCANAP2' => '50629',
          'PCANAP5' => '50636',
          'PCANAP6' => '50635',
          'PCANAP7' => '50634',
          'PCANAP8' => '50633',
          'FZD4' => '8322',
          'PLA2G2A' => '5320',
          'PTPN12' => '5782',
          'SDCCAG10' => '10283',
          'RB1' => '5925',
          'DBC1' => '1619',
          'CDH13' => '1012',
          'SDCCAG16' => '10813',
          'AXIN2' => '8313',
          'CTNNB1' => '1499',
          'TCF7L2' => '6934',
          'PPP1R14C' => '81706',
          'SDBCAG84' => '51614',
          'NCOA3' => '8202',
          'DNTT' => '11259',
          'TSG101' => '54749',
          'PHB' => '5245',
          'ACVR1B' => '91',
          'HRAS' => '3265',
          'MSH2' => '4436',
          'SDCCAG28' => '10809',
          'MSH6' => '2956',
          'TGFBR2' => '7048',
          'PDGFRL' => '5157',
          'BLCAP' => '55508',
          'PIK3CA' => '5290',
          'MAP3K8' => '1326',
          'MXI1' => '91563',
          'TFF1' => '7031',
          'SNCG' => '6623',
          'PNMA2' => '10687',
          'SDCCAG31' => '10811',
          'MCC' => '29919',
          'SDCCAG33' => '10194',
          'AR' => '367',
          'RFC1' => '7864'
        );

# Now we can finish writing the program that searches for a gene in a hash:

print $hash{'TFF1'},"\n";
my $query = '';

until($query =~ /^quit/) {

	print "Official symbol for gene name (or 'quit'): ";
	$query = <STDIN>;
	chomp $query;

	if(defined $hash{$query}) {
		print "Gene $query has locuslink id ", $hash{$query},"\n";
	}else{
		print "Gene $query not in our database\n";
	}
}

exit;
